package com.hdjk.view.callback;

import com.hdjk.view.entity.WalletHoldingEntity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONArray;
import org.json.JSONObject;

import java.math.BigDecimal;

import okhttp3.Response;

/**
 * Created by sxy on 2017/11/2.
 */

public abstract class GetWalletHoldingsCallBack extends MsgCallBack {
    @Override
    public Msg parseNetworkResponse(Response response, int i) throws Exception {
        Msg msg = super.parseNetworkResponse(response, i);
        JSONObject object = (JSONObject) msg.getData();
        if (object != null) {
            WalletHoldingEntity entity = new WalletHoldingEntity();
            JSONArray array = object.getJSONArray("data");
            if (array != null && array.length() > 0) {
                JSONObject jsonObject = array.getJSONObject(0);
                if (jsonObject != null) {
                    if (!jsonObject.isNull("fundCode")) {
                        entity.setFundCode(jsonObject.getString("fundCode"));
                    }
                    if (!jsonObject.isNull("fundName")) {
                        entity.setFundName(jsonObject.getString("fundName"));
                    }
                    if (!jsonObject.isNull("totalCurrentValue")) {
                        double l = jsonObject.getDouble("totalCurrentValue");
                        BigDecimal b = new BigDecimal(l);
                        entity.setTotalCurrentValue(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    }
                    if (!jsonObject.isNull("intransitAssets")) {
                        entity.setIntransitAssets(jsonObject.getString("intransitAssets"));
                    }
                    if (!jsonObject.isNull("profitLoss")) {
                        double l = jsonObject.getDouble("profitLoss");
                        BigDecimal b = new BigDecimal(l);
                        entity.setProfitLoss(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    }
                    if (!jsonObject.isNull("preProfitLoss")) {
                        double l = jsonObject.getDouble("preProfitLoss");
                        BigDecimal b = new BigDecimal(l);
                        entity.setPreProfitLoss(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    }
                    if (!jsonObject.isNull("showedDate")) {
                        entity.setShowedDate(jsonObject.getString("showedDate"));
                    }
                    if (!jsonObject.isNull("availableValue")) {
                        double l = jsonObject.getDouble("availableValue");
                        BigDecimal b = new BigDecimal(l);
                        entity.setAvailableValue(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    }
                    if (!jsonObject.isNull("undistributeMonetaryIncome")) {
                        entity.setUndistributeMonetaryIncome(jsonObject.getString("undistributeMonetaryIncome"));
                    }
                    if (!jsonObject.isNull("accumulatedProfitLoss")) {
                        double l = jsonObject.getDouble("accumulatedProfitLoss");
                        BigDecimal b = new BigDecimal(l);
                        entity.setAccumulatedProfitLoss(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                    }
                    if (!jsonObject.isNull("investmentAmount")) {
                        entity.setInvestmentAmount(jsonObject.getString("investmentAmount"));
                    }
                }
            }
            msg.setData(entity);
        }
        return msg;
    }
}
